<?php

/**
 * This is the model class for table "{{pc_sms}}".
 * 短息接口开发 ：李伟乐
 * 例子：
 *  参数：
 *  mobile 必选 手机号
 *  message 必选 短信内容
 *  invite_code 可选 验证码
 *  aa_user_sn 可选 用户aa
 * $data=array('mobile'=>"15300295509","message"=>"智慧教育");
    $sms=new ZSms;
    echo $sms->send($data);
 * 返回 成功 1 失败 0
 * The followings are the available columns in table '{{pc_sms}}':
 * @property string $id
 * @property string $mobile
 * @property string $message
 * @property integer $time_send
 * @property integer $time_created
 * @property integer $is_send
 * @property string $return_result
 * @property integer $aa_user_sn
 * @property string $invite_code
 */
class ZSms {

    var $userType = 1;
    //var $companyMobile=95165418418;
    //var $tpaKey="hqzk2012";
    //var $employeeNo=626;

    var $format = "json";
    var $api_url = "http://202.91.247.165:8081/webApi/services/rest";
    var $companyMobile = 9516567890;
    var $tpaKey = "hqzk2012";
    var $employeeNo = 6001;

    public function getSession() {
        $cookie = tempnam('./tmp/', 'cookie');
        $rand = rand(10000, 99999);
        $cDate = date("Y-m-d H:i:s");
        $ctime = date("YmdHis", strtotime($cDate));
        $tpaKey = $this->tpaKey;
        $transID = $tpaKey . $ctime . $rand;
        $data = array(
            'userType' => $this->userType,
            'companyMobile' => $this->companyMobile,
            'employeeNo' => $this->employeeNo,
            'method' => "login",
            'tpaKey' => $this->tpaKey,
            'format' => $this->format,
            'transID' => $transID,
            'timestamp' => $cDate
        );
        $ch = curl_init($this->api_url);
        $header = "Content-type: text/xml"; //;
        //curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//设置HTTP头 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $ret = curl_exec($ch);
        curl_close($ch);
        if (empty($ret))
            return false;
        $session = json_decode($ret);
        if (isset($session->login_response)) {
            $result = (array) $session->login_response;
            $result['timestamp'] = $cDate;
            return $result;
        }
        return false;
    }
    
    /**
     * 发送单条短信
     * 
     * @version 1.0
     * @date 2013-08-05
     */
    public function send($dataSend) {

        if (!isset($dataSend['mobile']) || !isset($dataSend['message']))
            return null;

        $result = $this->getSession();
        $data = array(
            'timestamp' => $result['timestamp'],
            'transID' => $result['transID'],
            'tpaKey' => $this->tpaKey,
            'sendType' => 0,
            'sessionKey' => $result['sessionKey'],
            'format' => $this->format,
            'method' => "sendSms",
            'phones' => $dataSend['mobile'],
            'content' => $dataSend['message']
        );
        $cookie = tempnam('./tmp/', 'cookie');
        $ch = curl_init($this->api_url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $ret = curl_exec($ch);
        curl_close($ch);

        return $result['timestamp'];
    }
    
    /**
     * 群发短信
     * 
     * @version 1.0
     * @date 2013-08-05
     */
    public function sendgroup($dataSend) {

        if (!isset($dataSend['mobiles']) || !isset($dataSend['message']) || !isset($dataSend['aa_user_sn']))
            return null;

        $result = $this->getSession();
        $data = array(
            'timestamp' => $result['timestamp'],
            'transID' => $result['transID'],
            'tpaKey' => $this->tpaKey,
            'sendType' => 0,
            'sessionKey' => $result['sessionKey'],
            'format' => $this->format,
            'method' => "sendSms",
            'phones' => $dataSend['mobiles'],
            'content' => $dataSend['message'],
        );
        $cookie = tempnam('./tmp/', 'cookie');
        $ch = curl_init($this->api_url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $ret = curl_exec($ch);
        curl_close($ch); 

        return $result['transID'];
    }
    
    /**
     * 发送短信列表
     * 
     * @version 1.0
     * @date 2013-08-05
     */
    public function smsList($data){
        if(empty($data))
            return null;
        
        $result = $this->getSession();
        $data = array(
            'timestamp' => $result['timestamp'],
            'transID' => $result['transID'],
            'tpaKey' => $this->tpaKey,
            'sessionKey' => $result['sessionKey'],
            'format' => $this->format,
            'method' => "querySmsHis",
            'begin_time' => $data['begin_time'],
            'end_time' => $data['end_time'],
            //'dst_phone' => $data['phone'],
           // 'query_star' => $data['page_num'],
           // 'query_length' => $data['page_total'],
            'need_details' => 0
        );
        $cookie = tempnam('./tmp/', 'cookie');
        $ch = curl_init($this->api_url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $ret = curl_exec($ch);
        curl_close($ch);
        
        echo  $result['transID'];
        return $ret;
    }
    
    /**
     * 查看回复短信的列表
     * 
     * @version 1.0
     * @date 2013-08-05
     */
    public function smsUpList($data) {
        if(empty($data))
            return null;
        
        $result = $this->getSession();
        $data = array(
            'timestamp' => $result['timestamp'],
            'transID' => $result['transID'],
            'tpaKey' => $this->tpaKey,
            'sessionKey' => $result['sessionKey'],
            'format' => $this->format,
            'method' => "queryUpSms",
            'begin_time' => $data['begin_time'],
            'end_time' => $data['end_time'],
            //'dst_phone' => $data['phone'],
            //'query_star' => $data['page_num'],
            //'query_length' => $data['page_total'],
            //'need_details' => 1
        );
        $cookie = tempnam('./tmp/', 'cookie');
        $ch = curl_init($this->api_url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $ret = curl_exec($ch);
        curl_close($ch);
        
        return $ret;
    }
    
    /**
     * 发送短信
     * 获取发送短信条数
     * 
     * @version 1.0
     * @author wjy
     * @date 2013-05-20
     */
    
    public function insertSchoolSms($school,$sms_group_id,$aa_user_sn,$content,$time_created,$cache_data,$sendRange){
        if(!$school || !$sms_group_id || !$aa_user_sn || !$content || !$time_created || !$cache_data || !$sendRange)
            return false;
        
        $model = new DMSms($school);
        $model->aa_user_sn = $aa_user_sn;
        $model->cache_data = $cache_data;
        $model->sms_group_id = $sms_group_id;
        $model->content = $content;
        $model->time_created = $time_created;
        $model->send_range = $sendRange;
        if($model->save()){
            return $model->id;
        }else{
            return 0;
        }
    }

}